let app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        select:0,
        move:0,
        selectList:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let d = app.globalData;
        this.setData({
            platform:d.platform,    
            //open:open,
            width:d.windowWidth,
            height:d.windowHeight,
            statusBarHeight:d.statusBarHeight,
            navHeight:d.navHeight,
            navWidth:d.navWidth,
            navRight:d.navRight,
            marginTop:d.navTitleTop,
            domain:d.domain,
            uploadUrl:d.uploadUrl,
          })
    },
    goBack(){
        wx.navigateBack();
    },
    goIndex(){
        var  url ='/pages/customer/index';
        console.log(url);
        wx.reLaunch({
          url:url
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getIndex();
    },
    getIndex(){
        let that = this;
        var open =  wx.getStorageSync('open');
        var depart = wx.getStorageSync('depart');
        var depart_id = depart.id;
        //var depart_id = 26;
        var data = [];
        data['user_id']= open.id;
        data['depart_id']= depart_id;
        var url = 'wechat/customer/getPubList';

      app.request(url,data,'get').then(res=>{ 
        console.log('---customer ----getPub'); 
         console.log(res);
         that.setData({
             list:res.list,
            
         })
      });
    },
    boxChange(e){
        console.log(e);
        var selectList = e.detail.value;
          var select = selectList.length;
        if(selectList.length==0){
            selectList.push(v);
        }
        this.setData({
            select:select,
            selectList:selectList
        })
        //console.log(select)
    },

    putPub(){
        var d = this.data;
        let that = this;
        var open =  wx.getStorageSync('open');
        var data = [];
        data['user_id'] = open.user_id;
        if(d.select == 0){
           // console.log();
           app.showModals('请选择客户','#','#');
            return;
        }
        var list= d.selectList.toString();
        console.log(list);
        data['list'] = list;
        console.log(data)
        var url = 'wechat/customer/toPub';

      app.request(url,data,'get').then(res=>{ 
        console.log('---customer ----lose to reject'); 
         console.log(res);
         app.showModals(res.msg,res.url,res.index);
         that.getIndex();
      });
    },
    move(){
        var d = this.data;
        let that = this;
        var depart =  wx.getStorageSync('depart');
        var data = [];
        data['depart_id'] = depart.id;
        if(d.select == 0){
           // console.log();
           app.showModals('请选择客户','#','#');
            return;
        }
        var url = 'wechat/customer/getDepartUser';

        app.request(url,data,'get').then(res=>{ 
          console.log('---customer ----getDepartUser'); 
           console.log(res);
           var list = res.store;
           var len=0;
           console.log(list);
           len = list.length;
           console.log(len);
           var height =48+41*len;
           that.setData({
               store:list,
               needH:height,
               storeH:height,
               move:1,
               //store:res.store
           })
          // app.showModals(res.msg,res.url,res.index);
          // that.getIndex();
        });


      

    },
    selectUser(e){
        
        var  user_id = e.detail.value;
        console.log(user_id)
        this.setData({
            user_id:user_id
        })
    },
    toMove(e){
        var d = this.data;
        var selectList = d.selectList;
        let that = this;
        console.log(d.user_id)
        if(d.user_id == undefined){
            // console.log();
            app.showModals('请选择员工','#','#');
             return;
         }
         var data = [];
         data['user_id'] = d.user_id;
         data['list'] = selectList.toString();
         var url = 'wechat/customer/pubToUser';

         app.request(url,data,'get').then(res=>{ 
           console.log('---customer ----pub to User'); 
            console.log(res);
            app.showModals(res.msg,res.url,res.index);
            that.setData({
                user:'',
                needH:0,
                move:0,
                selectList:[],

            })
            that.getIndex();
         });

    },
    cancleMove(){
        this.setData({
            user:'',
            needH:0,
            move:0
        })
    },
    openOrClose(e){
        let d = this.data;
        var id = e.currentTarget.dataset.id;
        var store = d.store;
        console.log(id);
        console.log(store);
        if(d.select == 0){
            // console.log();
            app.showModals('请选择客户','#','#');
             return;
         }
        store.forEach((item, index) => {
            //console.log(item)
            //console.log(index)
            if(item.id==id){
                if(item.icon=='open'){
                    item.icon='close';
                    this.getUser(id);
                }else{
                    item.icon='open'
                }
            }else{
                item.icon='open'
            }
          });
        this.setData({
            store:store,
            now_store_id:id
        })
    },
    getUser(id){
        var d = this.data;
        let that = this;
        var depart =  wx.getStorageSync('depart');
        var data = [];
        data['store_id'] = id;
 
        var url = 'wechat/customer/getStoreUser';

        app.request(url,data,'get').then(res=>{ 
          console.log('---customer ----getStoreUser'); 
           console.log(res);
           var list = res.list;
           var len=0;
           console.log(list);
           len = list.length;
           console.log(len);
           var storeH= that.data.storeH;
           var height = storeH+16*len;
           that.setData({
               user:list,
               needH:height,
               move:1,
               //store:res.store
           })
          // app.showModals(res.msg,res.url,res.index);
          // that.getIndex();
        });


      

    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})